package com.wanlian.router.ui.manage;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.widget.TextView;

import com.smart.android.ui.BaseFragment;
import com.smart.android.ui.app.IntentExtra;
import com.smart.android.widget.EditTextWithClear;
import com.wanlian.router.R;
import com.wanlian.router.bean.RouterInfo;
import com.wanlian.router.bean.WifiSetting;
import com.wanlian.router.net.NetWorkingClient;
import com.wanlian.router.net.ResponseData;
import com.wanlian.router.net.internal.INetCallBack;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * Created by Hyu on 2018/9/8.
 * Email: fvaryu@163.com
 */
public class IPSettingFragment extends BaseFragment implements TextWatcher {

    @BindView(R.id.et_ip)
    EditTextWithClear et_ip;
    @BindView(R.id.et_mask)
    EditTextWithClear et_mask;
    @BindView(R.id.et_gateway)
    EditTextWithClear et_gateway;

    @BindView(R.id.et_dns)
    EditTextWithClear et_dns1;
    @BindView(R.id.et_dns2)
    EditTextWithClear et_dns2;

    @BindView(R.id.tv_next)
    TextView next;

    public static IPSettingFragment newInstance(int type) {
        Bundle args = new Bundle();
        args.putInt(IntentExtra.EXTRA_TYPE, type);
        IPSettingFragment fragment = new IPSettingFragment();
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    protected int layout() {
        return R.layout.fragment_ip_setting;
    }

    @Override
    protected void initUI(View view) {
        super.initUI(view);
        et_gateway.addTextChangedListener(this);
        et_mask.addTextChangedListener(this);
        et_ip.addTextChangedListener(this);

        et_dns1.addTextChangedListener(this);

    }

    @Override
    protected void initData(View view) {
        super.initData(view);

        NetWorkingClient.getInstance().queryCurrentRouterInfo(getActivity(), new INetCallBack<RouterInfo>() {
            @Override
            public void onFinish(ResponseData response, @Nullable RouterInfo info) {
                if (response.isSuccess()) {
                    if (info != null) {
                        if (info.getDeviceConfig() == null) {
                            return;
                        }
                        WifiSetting wifiSetting = info.getDeviceConfig();
                        et_ip.setText(wifiSetting.getStatic_webAddress());
                        et_mask.setText(wifiSetting.getStatic_subnetMask());
                        et_gateway.setText(wifiSetting.getStatic_defaultNetWay());
                        et_dns1.setText(wifiSetting.getOneDNS());
                        et_dns2.setText(wifiSetting.getTwoDNS());
                    }
                }
            }
        });
    }

    private boolean v(String string) {
        if (TextUtils.isEmpty(string)) {
            return false;
        }
        String regex = "(2[5][0-5]|2[0-4]\\d|1\\d{2}|\\d{1,2})\\.(25[0-5]|2[0-4]\\d|1\\d{2}|\\d{1,2})\\.(25[0-5]|2[0-4]\\d|1\\d{2}|\\d{1,2})\\.(25[0-5]|2[0-4]\\d|1\\d{2}|\\d{1,2})";
        return string.matches(regex);
    }

    @OnClick(R.id.tv_next)
    void next() {
        if ((et_dns2.getText() != null && !TextUtils.isEmpty(et_dns2.getText().toString())) && v(et_dns2.getText().toString())) {
            showToast("请输入有效DNS");
            return;
        }

        WifiSetting wifiSetting = new WifiSetting();
        wifiSetting.setSord(getArguments().getInt(IntentExtra.EXTRA_TYPE) == 1 ? "static" : "dhcp");
        wifiSetting.setDynamic_defaultNetWay(et_gateway.getText().toString());
        wifiSetting.setDynamic_subnetMask(et_mask.getText().toString());
        wifiSetting.setDynamic_webAddress(et_ip.getText().toString());
        wifiSetting.setStatic_defaultNetWay(et_gateway.getText().toString());
        wifiSetting.setStatic_subnetMask(et_mask.getText().toString());
        wifiSetting.setStatic_webAddress(et_ip.getText().toString());
        wifiSetting.setOneDNS(et_dns1.getText().toString());
        wifiSetting.setTwoDNS(et_dns2.getText().toString());
        NetWorkingClient.getInstance().settingWifi(getActivity(), false, false, wifiSetting, new INetCallBack() {
            @Override
            public void onFinish(ResponseData response, @Nullable Object o) {
                if (response.isSuccess()) {

                    showToast("设置成功");
                } else {
                    showToast(response.getMessage());
                }
            }
        });
    }

    @Override
    public void beforeTextChanged(CharSequence s, int start, int count, int after) {

    }

    @Override
    public void onTextChanged(CharSequence s, int start, int before, int count) {

    }

    @Override
    public void afterTextChanged(Editable s) {


        next.setEnabled(
                et_ip.getText() != null && v(et_ip.getText().toString()) &&
                        et_mask.getText() != null && v(et_mask.getText().toString()) &&
                        et_dns1.getText() != null && v(et_dns1.getText().toString()) &&
                        et_gateway.getText() != null && v(et_gateway.getText().toString())

        );

    }
}
